﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.WebFoundation.Controls.Runtime;
using Skynet.BusinessEntities;

namespace Skynet.WebAdministracion.Views.ExamViewer
{
    public class ExamViewerPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IExamViewerView>
    {
        [Dependency]
        public WebFoundation.Services.Session.ISessionService SessionService { get; set; }

        [Dependency]
        public WebFoundation.Services.Navigation.INavigationService NavigationService { get; set; }


        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }


        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }

        public override void OnViewLoaded()
        {
            var questions = this.SessionService.RuntimeExamQuestions;
            var position = this.SessionService.RuntimeExamQuestionPosition;
            var question = questions.ElementAt(position);
            this.BCExamQueries.LoadQuestion(BusinessLogic.BusinessComponents.LoadQuestionOptions.LoadRuntimeQuestion, ref question);
            List<System.Web.UI.Control> Controls = new List<System.Web.UI.Control>();
            if (question is BusinessEntities.TextQuestion)
            {
                var target = question as BusinessEntities.TextQuestion;
                var control = new WebFoundation.Controls.Runtime.TextQuestionControl();
                control.SkinID = "TextQuestionRuntime";
                control.Text = target.Question;                
                this.View.QuestionControl = control;
            }
            for (int i = 0; i < question.Alternatives.Count; i = i + 1)
            {
                var alternative = question.Alternatives.ElementAt(i);
                this.BCExamQueries.LoadAlternative(BusinessLogic.BusinessComponents.LoadAlternativeOptions.LoadCustomerAnswer, ref alternative);
                this.BCExamQueries.LoadAlternative(BusinessLogic.BusinessComponents.LoadAlternativeOptions.LoadDesignAlternative, ref alternative);
                if (alternative is BusinessEntities.BoolAlternative)
                {
                    var target = alternative as BusinessEntities.BoolAlternative;
                    var alt = new WebFoundation.Controls.Runtime.BoolAlternativeControl();                                                            
                    alt.AlternativeCustomerId = target.CustomerAlternativeId;
                    alt.SkinID = "BoolAlternativeRuntime";                    
                    Controls.Add(alt);
                }
            }
            this.View.AlternativesControls = Controls;
        }
        public override void OnViewInitialized()
        {
            var questions = this.SessionService.RuntimeExamQuestions;
            var position = this.SessionService.RuntimeExamQuestionPosition;

            var exam = this.BCExamQueries.SearchExamByCustomer(BusinessLogic.BusinessComponents.ExamByCustomerSearchOptions.GetByExamByCustomerId, ExamByCustomerId: this.SessionService.RuntimeExamByCustomerId).SingleOrDefault();
            var question = questions.ElementAt(position);
            this.BCExamQueries.LoadQuestion(BusinessLogic.BusinessComponents.LoadQuestionOptions.LoadRuntimeQuestion, ref question);
            this.View.CurrentPosition = position;
            this.View.TotalQuestions = questions.Count;
            this.View.EndExam = exam.StartTime.Value.AddMinutes(exam.Duration);
            this.View.Review = question.Review;



            this.BCExamQueries.LoadQuestion(BusinessLogic.BusinessComponents.LoadQuestionOptions.LoadRuntimeQuestion, ref question);
            List<System.Web.UI.Control> Controls = new List<System.Web.UI.Control>();
            if (question is BusinessEntities.TextQuestion)
            {
                var target = question as BusinessEntities.TextQuestion;
                var control = new WebFoundation.Controls.Runtime.TextQuestionControl();
                control.SkinID = "TextQuestionRuntime";
                control.Text = target.Question;
                this.View.QuestionControl = control;
            }
            for (int i = 0; i < question.Alternatives.Count; i = i + 1)
            {
                var alternative = question.Alternatives.ElementAt(i);
                this.BCExamQueries.LoadAlternative(BusinessLogic.BusinessComponents.LoadAlternativeOptions.LoadCustomerAnswer, ref alternative);
                this.BCExamQueries.LoadAlternative(BusinessLogic.BusinessComponents.LoadAlternativeOptions.LoadDesignAlternative, ref alternative);
                if (alternative is BusinessEntities.BoolAlternative)
                {
                    var target = alternative as BusinessEntities.BoolAlternative;
                    var alt = new WebFoundation.Controls.Runtime.BoolAlternativeControl();
                    alt.Answer = target.CustomerAnswer;
                    alt.AlternativeCustomerId = target.CustomerAlternativeId;
                    alt.SkinID = "BoolAlternativeRuntime";
                    Controls.Add(alt);
                }
            }
            this.View.AlternativesControls = Controls;
        }

        private void UpdateRegister()
        {
            var position = this.SessionService.RuntimeExamQuestionPosition;
            var questions = this.SessionService.RuntimeExamQuestions;
            var question = questions.ElementAt(position);
            question.Review = this.View.Review;
            this.BCExams.MaintenanceQuestions(BusinessLogic.BusinessComponents.MaintenanceQuestionsOptions.UReview, question);
            var controls = this.View.AlternativesControls;
            foreach (var item in controls)
            {
                if (item is BoolAlternativeControl)
                {
                    var target = item as BoolAlternativeControl;
                    var targetalt = (from c in question.Alternatives where c.CustomerAlternativeId == target.AlternativeCustomerId select c).SingleOrDefault() as BoolAlternative;
                    targetalt.CustomerAnswer = target.Answer;
                    this.BCExams.MaintenanceAlternative(BusinessLogic.BusinessComponents.MaintenanceAlternativeOptions.UCustomerAnswer, targetalt);
                }
            }            
            this.SessionService.RuntimeExamQuestions = questions;                  
        }
        public void Next()
        {
            this.UpdateRegister();
            var position = this.SessionService.RuntimeExamQuestionPosition;
            var questions = this.SessionService.RuntimeExamQuestions;            
            if (position + 1 < questions.Count)
                position += 1;        
            this.SessionService.RuntimeExamQuestionPosition = position;
            this.NavigationService.Navigate("~/Evaluation/ExamViewer.aspx");
        }
        public void Previus()
        {
            this.UpdateRegister();
            var position = this.SessionService.RuntimeExamQuestionPosition;            
            if (position > 0)
                position -= 1;
            this.SessionService.RuntimeExamQuestionPosition = position;
            this.NavigationService.Navigate("~/Evaluation/ExamViewer.aspx");
        }
        public void ShowReview()
        {
            this.UpdateRegister();
            this.NavigationService.Navigate("~/Evaluation/ExamReviewViewer.aspx");
        }
    }
}
